package com.shrcl.engineerprocess.domain.impl;

import com.google.common.base.Objects;
import com.shrcl.engineerprocess.domain.Engineer;
import com.shrcl.engineerprocess.domain.EngineerTraining;
import com.shrcl.engineerprocess.domain.EngineerTrainingDAO;
import com.shrcl.engineerprocess.domain.impl.EngineerTrainingImpl;
import edu.fudan.mylang.pf.BaseDAOService;
import edu.fudan.mylang.pf.IGenericQuery;
import java.util.Collection;
import java.util.List;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.stereotype.Service;

@Service("com.shrcl.engineerprocess.domain.EngineerTrainingDAO")
public class EngineerTrainingDAOImpl extends BaseDAOService<EngineerTraining> implements EngineerTrainingDAO {
  public EngineerTraining create(final Procedure1<? super EngineerTraining> init) {EngineerTraining engineerTraining=EngineerTrainingImpl.create(getObjectFactory());
    final Procedure1<EngineerTraining> _function = new Procedure1<EngineerTraining>() {
        public void apply(final EngineerTraining it) {
          init.apply(it);
          it.start();
        }
      };
    EngineerTraining _doubleArrow = ObjectExtensions.<EngineerTraining>operator_doubleArrow(engineerTraining, _function);
    return _doubleArrow;
  }
  
  public void delete(final EngineerTraining engineerTraining) {getObjectFactory().delete(engineerTraining);
    
  }
  
  public Collection<EngineerTraining> getAllTrainingsByEngineer(final Engineer engineer) {IGenericQuery<EngineerTraining> query=getObjectFactory().createGenericQuery(EngineerTraining.class,"select e from com.shrcl.engineerprocess.domain.impl.EngineerTrainingImpl as e   where  e.active = 1      ");
    Collection<EngineerTraining> _list = query.list();
    final Function1<EngineerTraining,Boolean> _function = new Function1<EngineerTraining,Boolean>() {
        public Boolean apply(final EngineerTraining it) {
          Collection<Engineer> _trainees = it.getTrainees();
          final Function1<Engineer,Boolean> _function = new Function1<Engineer,Boolean>() {
              public Boolean apply(final Engineer it) {
                boolean _equals = Objects.equal(it, engineer);
                return Boolean.valueOf(_equals);
              }
            };
          Engineer _findFirst = IterableExtensions.<Engineer>findFirst(_trainees, _function);
          boolean _notEquals = (!Objects.equal(_findFirst, null));
          return Boolean.valueOf(_notEquals);
        }
      };
    Iterable<EngineerTraining> _filter = IterableExtensions.<EngineerTraining>filter(_list, _function);
    List<EngineerTraining> _list_1 = IterableExtensions.<EngineerTraining>toList(_filter);
    return _list_1;
  }
}
